package cn.jilijili;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;

import javax.xml.ws.spi.http.HttpHandler;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : IndexController  //类名
 * @Description :   //描述
 * @Author : HTB  //作者
 * @Date: 2020-04-25 09:35  //时间
 */
public class IndexController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/index")
    public String index(){
        return "index";

    }
    @GetMapping("/authorization_code")
    public String authorization_code(String code){
        Map<String,String> map = new HashMap<>();
        map.put("client_id","16b9874839eb512acd8f");
        map.put("client_secret","71a642da7f81ba565fc5681ab031a6d11e5be4a0");
        map.put("state","Real-PSG");
        map.put("code",code);
        map.put("redirect_uri","http://localhost:8080/authorization_code");

        Map<String,String> resp = restTemplate.postForObject("https://github.com/login/oauth/access_token", map, Map.class);
        System.out.println(resp);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization","token"+resp.get("access_token"));
        HttpEntity<Object> httpEntity = new HttpEntity<>(headers);
        ResponseEntity<Map> exchange = restTemplate.exchange("https://api.github.com/user", HttpMethod.GET, httpEntity, Map.class);
        System.out.println(exchange.getBody());
        return "forward:/index.html";



    }
}
